package com.snbc.util;

import java.io.IOException;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	private static SimpleDateFormat formatter=new SimpleDateFormat("yyyyMMddHHmmss");

	public static String monthDay;

	public static long dateToLong(String date){
		if(date==null){
			return System.currentTimeMillis();
		}else{
			Date d=parserDate(date);
			return d.getTime();
		}
	}
	
	public static String longtoDate(long milltimes){
		Date date = new Date(milltimes);
	    return formatter.format(date);
	}
	   
	public static long dateToLong(String date, SimpleDateFormat formatter){
		ParsePosition pos = new ParsePosition(0);
		return formatter.parse(date, pos).getTime();
	}
	
	public static long dateToLong(String date, String format){
		ParsePosition pos = new ParsePosition(0);
		return new SimpleDateFormat(format).parse(date, pos).getTime();
	}
	public static Date parserDate(String date){
		ParsePosition pos = new ParsePosition(0);
		return formatter.parse(date, pos);
	}
	public static String getNowYearMonDay(){
		return new SimpleDateFormat("yyyyMMdd").format(new Date());
	}

	public static void main(String[] args) throws IOException {
		String s = KeyGenerate.getKeyPrefix("20190716021223");
		System.out.println(s);
	}
    public static Date getStartTime(String year_mon_day) throws ParseException {
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(sdf.parse(year_mon_day));
        //todayStart.add(Calendar.DAY_OF_MONTH, -1);
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();  
    }  
    public static Date getnowEndTime(String year_mon_day) throws ParseException {
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.setTime(sdf.parse(year_mon_day));
        //todayEnd.add(Calendar.DAY_OF_MONTH, -1);
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();  
    }

    public static void initMMDD(int y,int m,int d) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(y,m-1,d,0,0,0);
		long l = (calendar.getTime().getTime()-1561867200000l)/86400000;
		long h = l/676+97;
		long i1 = (l%676)/26+97;
		long i2 = (l%676)%26+97;
		byte[] bytes = {(byte)h,(byte)i1,(byte)i2};
		monthDay = new String(bytes);
	}



}
